<?php

class CreditController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		$this->commonAction();	
	}
	
	
	public function indexAction(){
		try {
			$sql="SELECT * FROM `setting`";
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opsaveAction(){
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'parameter_error' );
			}
			$p =$this->_request->getPost ();
			
			if(!$p['name'] || !$p['score'] || !$p['content'] ){
				throw new Exception ( '内容不能为空' );
			}
			
			$data=array(
				'goodsname'=>$p['name'],
				'credit'=>$p['score'],
				'isdown'=>1,
				'addtime'=>time(),
				'intro'=>$p['content'],
			);
			
		    if(isset($_FILES['pic_url']['tmp_name']) && !empty($_FILES['pic_url']['tmp_name'])){
				  if($_FILES['pic_url']['type'] == 'image/jpeg'){ 
					 $type = '.png'; 
				  }else{
					 $type='.png';
				  }
				  $dirname=PROJECT_ROOT .'/public/credit/';	
				  if (!file_exists($dirname)) {
					  mkdir($dirname);
				  }  
				  //新文件名
				  $new_file_name_tmp = date("YmdHis") . '_' . rand(10000, 99999);
				  //移动文件 
				  $file_path = $dirname .'/'. $new_file_name_tmp;   
				  $new_file_name = $new_file_name_tmp.$type;//原图
				  $thumbname_small_tiny = $new_file_name_tmp.'.tiny'.$type;//  小图 150*150
				  $thumbname_big_tiny = $new_file_name_tmp.'.big'.$type;//  小图 150*150
				 // $thumbname_small = $new_file_name_tmp.'.'.$file_ext.'.small.'.$file_ext;//小图 300*300；
				 // $thumbname_source = $new_file_name_tmp.'.'.$file_ext.'.source.'.$file_ext;//大图 720*480；  小缩略图227*128，大缩略图660*1136
				  if( $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.'/'.$thumbname_small_tiny,128,227,4)){//小图   150* 150 		
					  $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.'/'.$thumbname_big_tiny,1136,660,4);					
					  $res=Azure_Storage::defaultImg('credit',$dirname.$thumbname_small_tiny,$thumbname_small_tiny);
					  $res=Azure_Storage::defaultImg('credit',$dirname.$thumbname_big_tiny,$thumbname_big_tiny);//上传到云服务器上面 1、文件夹的名字；2、文件的绝对路径；3、图片名字
					  
				  } 
				  $data['filename']=$thumbname_small_tiny;
				
/*				if($_FILES['pic_url']['type'] == 'image/jpeg'){ 
					$type = '.png'; 
				}else{
					$type='.png';
				}

				$dirname=PROJECT_ROOT .'/public/credit/'; 
				if (!file_exists($dirname)) {
					  mkdir($dirname);
			    }
				if(!file_exists($dirname)){		//如果$dirname不存在[每个学校不同的目录]，创建图片存储目录
					if(!$this->createFolder(PROJECT_ROOT .'/public/credit/',$dirname)){
						throw new Exception ( 'parameter_error！' );
					}
				}
				$rename = date("YmdHis") . '_' . rand(10000, 99999);
	
				$thumbname_big = $rename.".jpg";//原图
				$thumbname_small = $rename.".jpg.small.jpg";//小图
				$thumbname_source = $rename.".jpg.source.jpg";//大图 720*480；
				$this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.$thumbname_small,150,150,4);//手机缩略图
				$this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.$thumbname_big,860,330,2);//网页大图
				move_uploaded_file($_FILES['pic_url']['tmp_name'],$dirname.$thumbname_source);//原图
				
				$urls=PROJECT_ROOT .'/public/events/'.$settingArr['groupid'].'/';
				
				//print_r($_FILES);exit;
/*				if($_FILES['pic_url']['type'] == 'image/jpeg'){ 
					$type = '.png'; 
				}else{
					$type='.png';
				}
				$dirname=PROJECT_ROOT .'/public/credit/';	
				if (!file_exists($dirname)) {
					mkdir($dirname);
			 	}
				
				$rename = date("YmdHis") . '_' . rand(10000, 99999);
*/	
/*				$thumbname_big = $rename.".jpg";//原图
				$thumbname_small = $rename.".jpg.small.jpg";//小图
				$thumbname_source = $rename.".jpg.source.jpg";//大图 720*480；       小缩略图227*128，大缩略图660*1136
				$this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.$thumbname_small,128,227,4);//手机缩略图
				$this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.$thumbname_big,1136,660,2);//网页大图
				move_uploaded_file($_FILES['pic_url']['tmp_name'],$dirname.$thumbname_source);//原图
				
				$urls=PROJECT_ROOT .'/public/credit/';
				//echo $urls.$thumbname_big;exit;
				$res=Azure_Storage::defaultImg('credit',$urls.$thumbname_big,$thumbname);
				$res1=Azure_Storage::defaultImg('credit',$urls.$thumbname_small,$thumbname_small);
				$res2=Azure_Storage::defaultImg('credit',$urls.$thumbname_source,$thumbname_source);
				$yunfileurl='cloud.yunxiaoche.com';
				$yunfilepath='credit';
				$yunfilename=$rename;
				
				echo $res; echo $res1;echo $res2;exit;
				
				if($res&&$res1&&$res2){
					//删除本地图片
					unlink($urls.$thumbname_big);
					unlink($urls.$thumbname_small);
					unlink($urls.$thumbname_source);
				}else{
					throw new Exception ('失败');
				}
*/				//$data['pic_url']=$thumbname_small_tiny;
				$data['filesize']=$_FILES['pic_url']['size'];
				$data['fext']='jpg';
				  
	   		} 
			$result = $this->_dbAdapter->insert ( 'credit_goods', $data );
			$goodsid = $this->_dbAdapter->lastInsertId (); //获得刚插入的活动id
			
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/index' , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function publishedAction(){
		try {
			$sql="SELECT * FROM `credit_goods`";
			$result=$this->_dbAdapter ->fetchAll ( $sql );
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function editAction(){
		try {
			$goodsid = ( int ) $this->_request->getParam ( 'goodsid' );
	     	if (! $goodsid)
				throw new Exception ( '参数错误！' );
			$sql="SELECT * FROM `credit_goods` where goodsid='".$goodsid."'";
			$result=$this->_dbAdapter ->fetchAll ( $sql );
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}


	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

